package test

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper;

class JackJsonTestMain(
    var name: String = "22娘",
    var description: String = "姐姐，热情冒失"
) {


    companion object {
        @JvmStatic
        fun fun1SimpleTransform() {
            val objectMapper = ObjectMapper()

            val niang33 = JackJsonTestMain("33娘", "妹妹，冷淡腹黑")
            val strNiang33 = objectMapper.writeValueAsString(niang33)

            println("strNiang33 = $strNiang33")

            val niang33_1 = objectMapper.readValue(strNiang33, JackJsonTestMain::class.java)
            println("niang33_1  = $niang33_1, niang33_1 is JackJsonTestMain = ${niang33_1 is JackJsonTestMain}")
        }

        @JvmStatic
        fun fun2CollectionTransform() {
            val objectMapper = ObjectMapper()

            val array1 = arrayOf(JackJsonTestMain(), JackJsonTestMain("33娘", "妹妹，冷淡腹黑"))
            val strArray1 = objectMapper.writeValueAsString(array1)
            val array2 = objectMapper.readValue(strArray1, Array<JackJsonTestMain>::class.java)

            println("strArray1 = $strArray1")
            println("array2 = $array2, (array2  is Array<JackJsonTestMain>) is ${array2 is Array<JackJsonTestMain>}")
            println()

            val list1 = listOf(JackJsonTestMain(), JackJsonTestMain("33娘", "妹妹，冷淡腹黑"))
            val strList1 = objectMapper.writeValueAsString(list1)
            val list2: List<JackJsonTestMain> = objectMapper.readValue(
                strArray1,
                objectMapper.typeFactory.constructParametricType(
                    List::class.java,
                    JackJsonTestMain::class.java
                )
            )
            val list3: List<JackJsonTestMain> =
                objectMapper.readValue(strList1, object : TypeReference<List<JackJsonTestMain>>() {})

            println("strList1 = $strList1")
            println("list2 = $list2, (list2  is List<JackJsonTestMain>) is ${list2 is List<JackJsonTestMain>}")
            println("list3= $list3, (list3  is List<JackJsonTestMain>) is ${list3 is List<JackJsonTestMain>}")
        }
    }
}

fun main() {
//    JackJsonTestMain.fun1SimpleTransform()
    JackJsonTestMain.fun2CollectionTransform()
}